package com.hk.service.impl;

import com.hk.commons.entity.ActiveOrderRecord;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IActiveOrderRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by Sheldon Chen on 2017/2/7.
 */
@Service
public class ActiveOrderRecordImpl extends BaseServiceImpl<ActiveOrderRecord> implements IActiveOrderRecord {

    private Logger logger = LoggerFactory.getLogger(ActiveOrderRecordImpl.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(ActiveOrderRecord activeOrderRecord) {
        super.save(activeOrderRecord);
    }

    @Override
    public void update (ActiveOrderRecord activeOrderRecord) {
        super.update(activeOrderRecord);
    }

    @Override
    public ActiveOrderRecord query(Long userId, Long productId) {
        ActiveOrderRecord activeOrderRecord = null;

        String sql = "select * from t_active_order_record where userId = ? order by id desc";
        List<Map<String, Object>> results = null;
        try {
            results = this.jdbcTemplate.queryForList(sql, new Object[]{
                    userId
            });
            if (results != null && results.size() > 0) {
                Map<String, Object> result = results.get(0);

                activeOrderRecord = new ActiveOrderRecord();
                activeOrderRecord.setId((Long)result.get("id"));
                activeOrderRecord.setProductId((Long)result.get("productId"));
                activeOrderRecord.setUserId((Long)result.get("userId"));
                activeOrderRecord.setCount((Integer)result.get("count"));
            }
        } catch (Exception e) {
            logger.warn("the result is empty");
        }

        return activeOrderRecord;
    }
}
